//
//  SubUserManagerController.m
//  CarTown
//
//  Created by 熊浩 on 15/9/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "SubUserManagerController.h"
#import "SubUserTableViewCell.h"
#import "addSubUserTableViewCell.h"
#import "SubUserMode.h"
#import "AddSubUserMode.h"
#import "AddSubViewController.h"
#import "Puser.h"
#import <MJExtension.h>
#import "SubUserInfoUpdateTableViewController.h"
@interface SubUserManagerController ()<SubUserCelldelegate>
@property (nonatomic,strong) NSMutableArray *dataarray;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) MBProgressHUD *hud;
@end

@implementation SubUserManagerController
-(NSMutableArray *)dataarray{
    if (_dataarray==nil) {
        _dataarray=[NSMutableArray array];
    }
    return _dataarray;
}

-(void)updateState:(UPDATESTATE)state{
    
    if (state==STATESTART) {
        _hud =[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        _hud.labelText=@"正在更改";
    
        _hud.margin = 10.f;
        _hud.removeFromSuperViewOnHide = YES;
       
    }else if (state==STATESTOP){
        _hud.mode=MBProgressHUDModeText;
        _hud.labelText=@"更改成功";
        
        [_hud hide:YES afterDelay:1];
    }else if(state==STATEERROR){
        _hud.mode=MBProgressHUDModeText;
        _hud.labelText=@"网络错误";
        [_hud hide:YES afterDelay:1];
    }
}

+(instancetype)viewcontroller{
    SubUserManagerController *manager=[[SubUserManagerController alloc]initWithStyle:UITableViewStyleGrouped];
    return manager;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"子账号管理";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.tableView.backgroundColor=bgGarycolor;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self setupdata];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SubUserMode *mode=self.dataarray[indexPath.row];
    if ([mode isKindOfClass:[AddSubUserMode class]]) {
        return 52;
    }else{
        return 128;
    }
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

-(void)setupdata{
    
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"c_child";
    params[@"user_type"]=@"company";
    params[@"com_id"]=self.user.com_id;
    [PostUitls Post:userinfourl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        NSNumber *state=dict[@"state"];
        if ([state integerValue]==400) {
            NSArray *array=[SubUserMode objectArrayWithKeyValuesArray:dict[@"data"]];
            [self setuparray:array];
        }
    } failure:^(NSError *error) {
        [self setuparray:nil];
    }];
    
   
}
-(void)setuparray:(NSArray *)array{
    [self.dataarray removeAllObjects];
    
    if (array) {
        [self.dataarray addObjectsFromArray:array];
    }
    AddSubUserMode *mode=[[AddSubUserMode alloc]init];
    [self.dataarray addObject:mode];
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SubUserMode *mode=self.dataarray[indexPath.row];
    if ([mode isKindOfClass:[AddSubUserMode class]]) {
        AddSubViewController *vc=[AddSubViewController viewcontroller];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SubUserMode *mode=self.dataarray[indexPath.row];
    
    if ([mode isKindOfClass:[AddSubUserMode class]]) {
        addSubUserTableViewCell *cell=[addSubUserTableViewCell cellWithTableView:tableView];
        return cell;
    }else{
        SubUserTableViewCell *cell=[SubUserTableViewCell cellWithTableView:tableView];
        cell.delegate=self;
        cell.mode=mode;
        return cell;
    }
    // Configure the cell...
}

-(void)bianBtnClick:(SubUserTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    SubUserMode *mode=self.dataarray[indexpath.row];
    SubUserInfoUpdateTableViewController *vc=[[SubUserInfoUpdateTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    vc.mode=mode;
    [self.navigationController pushViewController:vc animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
